//
//  HttpHelperWithVideo.m
//  qiuShiJiangHu
//
//  Created by xalo on 16/1/26.
//  Copyright © 2016年 糗事江湖项目组. All rights reserved.
//

#import "HttpHelperWithVideo.h"
#import "VideoModel.h"
#import <AFHTTPSessionManager.h>
#import <AFNetworking/AFNetworking.h>

@interface HttpHelperWithVideo ()

@property (nonatomic,strong) NSMutableArray *modelArray;

@end


@implementation HttpHelperWithVideo

static HttpHelperWithVideo *hendel = nil;

//网络请求单例
+(HttpHelperWithVideo *)sharedManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        hendel = [[HttpHelperWithVideo alloc]init];
        
    });
    
    return hendel;
}


//数据解析
-(void)fectchSourceWithUrl:(NSString *)url block:(BLOCK)block
{
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",nil];
        
        [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSDictionary *dic = [NSDictionary dictionary];
            
            dic = responseObject;
            
            NSMutableArray *array = [NSMutableArray array];
            
            for (NSString *key in dic) {
                if ([key isEqualToString:@"items"]) {
                    
                    array = dic[@"items"];
                }
            }
            
            for (NSDictionary *dic1 in array) {
                
               VideoModel  *model = [[VideoModel alloc]init];
                
                [model setValuesForKeysWithDictionary:dic1];
                
                [self.modelArray addObject:model];
                
                //                NSLog(@"%@",_modelArray);
                
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                block();
            });
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            NSLog(@"请求失败:%@",error);
            
        }];
        
    });
    
}

//懒加载
-(NSMutableArray *)modelArray
{
    if (!_modelArray) {
        
        _modelArray = [NSMutableArray array];
        
    }
    
    return _modelArray;
}

//返回数组个数
-(NSInteger)countOfModelArray
{
    return _modelArray.count;
    
}


//根据下标返回对应的model
-(VideoModel *)returnModelWithIndex:(NSInteger)index
{
    return _modelArray[index];
    
}




















@end
